var mongoose = require('mongoose');
var Trainee = mongoose.model('Trainee');

exports.findAllTrainee = function (req, res, next) {
    // var trainees = [{
    //     domainId: 'WUJA13',
    //     name: '小吴',
    //     englishName: 'Jasper',
    //     major: 'Java',
    //     gender: '1234',
    //     teacher: 'LANCE ZHOU'
    // }];


    Trainee.find({},function(err, trainees){
        if(err){
            console.error(err);
            return;
        }
        return res.json(trainees);
    })
    

    // return res.json(trainees);
};

///根据 domainId ，获取 所选的trainee
exports.getTraineeByDomainId = function(req, res, next){

    var params = req.params;
    var uid = params.domainId;
    if(uid == null){
        return next(new Error('domainId is empty'));
    }
  Trainee.find({'domainId':uid},function(err, trainees){
      if(err){
          console.error(err);
           return next(new Error(err));
      }
      return res.json(trainees[0]);
  });
};


///新增条记录
exports.addTrainee = function(req, res, next){
    var body = req.body;
    if(body == null){
         return next(new Error('insert data is empty'));
    }
    var TraineeInstance = new Trainee(body);
    TraineeInstance.save((err,doc)=>{
        if(err){
            console.error(err);
           return next(new Error(err));
        }
        return res.json("success");
    });
}

///更新 记录 
exports.updateTrainee = function(req, res, next){
    // console.log(req);
    var body = req.body;
    if(body == null){
        return next(new Error('update data is empty'));
    }
    Trainee.updateMany(
        {domainId :body.domainId},
        {$set:body},
        function(err, result){
            if (err) return console.error(err);
            else return res.json('success update!!');
        }
    );
}